package cs422.project2.testing;

import java.awt.Panel;

import omicronAPI.OmicronAPI;
import omicronAPI.OmicronTouchListener;
import processing.core.PApplet;
import cs422.project2.listeners.TouchListener;

public class MainPage extends PApplet implements OmicronTouchListener
{
	PApplet applet;
	OmicronAPI omicronManager;
	TouchListener touchListener;
	Credits credit;
	
	int currentRegionalColumn = 0;
	
	int page = 0;
	int count = 0;
	
	int rowCount;
	int columnCount;
	
	int yearMin, yearMax; 
	int[] years;
	String[] movies;
	char[] countries;
	
	int yearInterval = 1;
	int yearIntervalText = 5;
	
	int volumeIntervalTEP = 20;
	int volumeIntervalMinorTEP = 10;
	int volumeIntervalTEC = 20;
	int volumeIntervalMinorTEC = 10;
	int volumeIntervalTEC_PC= 250;
	int volumeIntervalMinorTEC_PC = 125;
	int volumeIntervalTCDE= 2000;
	int volumeIntervalMinorTCDE = 1000;
	int volumeIntervalTCDE_PC= 30;
	int volumeIntervalMinorTCDE_PC = 15;
	int volumeIntervalTRE= 100;
	int volumeIntervalMinorTRE = 50;
	
	float plotTLX1, plotTLY1, plotTLX2, plotTLY2, labelTLX, labelTLY;
	float dataMinTEP, dataMinTEC, dataMinTEC_PC, dataMinTCDE, dataMinTCDE_PC, dataMinTRE;
	float dataMaxTEP, dataMaxTEC, dataMaxTEC_PC, dataMaxTCDE, dataMaxTCDE_PC, dataMaxTRE;
	float dataMinDemon, dataMaxDemon;
	FloatTable dataTEP, dataTEC, dataTEC_PC, dataTCDE, dataTCDE_PC, dataTRE, dataDemon;
//	double initialMillis = 0;
//	double count = 0;
//	static int index = 0;
	/**
	 * @param args
	 */
	
	public void init() 
	{
	  super.init();

	  // Creates the OmicronAPI object. This is placed in init() since we want to use fullscreen
	  omicronManager = new OmicronAPI(this);

	  // Removes the title bar for full screen mode (present mode will not work on Cyber-commons wall)
	 // omicronManager.setFullscreen(true);
	}
	
	public void setup()
	{
		size(1366,800);
		
		applet = this;
		credit = new Credits(this);
		
		dataTEP = new FloatTable(this, "../data/TEP_New.txt"); 
		dataTEC = new FloatTable(this,"../data/TEC_New.txt");
		dataTEC_PC = new FloatTable(this,"../data/TEC_PC_New.txt");
		dataTCDE = new FloatTable(this,"../data/TCDE_New.txt");
		dataTCDE_PC = new FloatTable(this,"../data/TCDE_PC_New.txt");
		dataTRE = new FloatTable(this,"../data/TRE_New.txt");
		dataDemon = new FloatTable(this, "../data/Demon.txt");
		
		//Corners of the Top Left Plotted Time Series
		  plotTLX1 = (float).1*width; 
		  plotTLX2 = (float).7*width;
		  labelTLX = (float).9*width;
		  plotTLY1 = (float).01*height;
		  plotTLY2 = (float).75*height;
		  labelTLY = (float).8*height;
		
		  //yearMin = PApplet.parseInt(dataDemon.getColumnMin(0));
		  //yearMax = PApplet.parseInt(dataDemon.getTableMax());

		  years = PApplet.parseInt(dataDemon.getRowNames());
		  yearMin = years[0];
		  yearMax = years[years.length - 1];
		  
		  dataMinTEP = 0;
		  dataMinTEC = 0;
		  dataMinTEC_PC = 0;
		  dataMinTCDE = 0;
		  dataMinTCDE_PC = 0;
		  dataMinTRE = 0;

		  dataMaxTEP = ceil(dataTEP.getTableMax() / volumeIntervalTEP) * volumeIntervalTEP;
		  dataMaxTEC = ceil(dataTEC.getTableMax() / volumeIntervalTEP) * volumeIntervalTEP;
		  dataMaxTEC_PC = ceil(dataTEC_PC.getTableMax() / volumeIntervalTEC_PC) * volumeIntervalTEC_PC;
		  dataMaxTCDE = ceil(dataTCDE.getTableMax() / volumeIntervalTCDE) * volumeIntervalTCDE;
		  dataMaxTCDE_PC = ceil(dataTCDE_PC.getTableMax() / volumeIntervalTCDE_PC) * volumeIntervalTCDE_PC;
		  dataMaxTRE = ceil(dataTRE.getTableMax() / volumeIntervalTRE) * volumeIntervalTRE;
		  
		  rowCount = dataDemon.getRowCount();
		  columnCount = dataDemon.getColumnCount();
		  
		  System.out.println(yearMin);
		  System.out.println(yearMax);
		  System.out.println(rowCount);
		  System.out.println(columnCount);
		  System.out.println(dataDemon.getMovieCount(2009));
		  //System.out.println(dataDemon.getMovieName());
		  //movies = dataDemon.getMovieNames(2008);
		  //System.out.println(movies[0]);
		// Create a listener to get events
		//touchListener = new TouchListener(this);
		  
		// Register listener with OmicronAPI
		omicronManager.setTouchListener(this);
	}

	public void draw()
	{		
		switch(page)
		{
		case 0:
			//System.out.println(dataDemon.getMovieCount(2008));
			//Black Background
			fill(0,0,0);
			rect(0,0,width,height);
			//2 White Buttons
			fill(255,255,255);			
			rect(width/4,height/4,width/4,height/2);
			rect(width/2,height/4,width/4,height/2);
			
			//Credit Button
			rect((float).8*width,(float).8*height,(float).1*width,(float).1*height);
			break;
		
		case 1:
			//Black Background
			fill(0,0,0);
			rect(0,0,width,height);
			//Red Button On Right
			fill(255,0,0);
			rect(width-width/4,height/4,width/5,height/2);
			
			//White Graph Rectangle
			fill(0,255,0);
			//rect(plotTLX1, plotTLY1, plotTLX2, plotTLY2);
			//rect((float).01*width,(float).01*width,(float).7*width,(float).8*height);
			this.noStroke();
			//drawAxisLabels("Test", plotTLX1, plotTLY1, plotTLX2, plotTLY2, labelTLX, labelTLY);
			//drawYearLabels(yearMin, yearMax, plotTLX1, plotTLY1, plotTLX2, plotTLY2);
			//drawVolumeLabels((int)dataMinTEP, (int)dataMaxTEP, volumeIntervalTEC, volumeIntervalMinorTEC, plotTLX1, plotTLY1, plotTLX1, plotTLY2);
			drawVolumeLabels((int)dataMinTEC, (int)dataMaxTEC, volumeIntervalTEC, volumeIntervalMinorTEC, plotTLX1, plotTLY2, plotTLX2, plotTLY2);
			//drawDataPointsTEP(currentRegionalColumn, (int)dataMinTEC, (int)dataMaxTEC, plotTLX1, plotTLY1, plotTLX2, plotTLY2,255,165,0);
			
			//drawYearLabels((int)dataMinTEC, (int)dataMaxTEC, plotTLX1, plotTLY1, plotTLX2, plotTLY2);
			break;
		
		case 2:
			//Black Background
			fill(0,0,0);
			rect(0,0,width,height);
			//Blue Button On Right
			fill(0,0,255);
			rect(width-width/4,height/4,width/5,height/2);	
			break;	
			
		case 3:
			credit.writeCredits();
			//Green Button On Right
			fill(0,255,0);
			rect(width-width/4,height/4,width/5,height/2);
			break;
		}
		omicronManager.process();
	}
	
	
	// Called on a touch down event
	// mousePressed events also call this with an ID of -1 and an xWidth and yWidth of 10.
	public void touchDown(int ID, float xPos, float yPos, float xWidth, float yWidth)
	{
		this.fill(255,0,0);
		this.noStroke();
		this.ellipse( xPos, yPos, xWidth, yWidth );
		// This is an optional call if you want the function call in the main applet class.
		// 'OmicronExample' should be replaced with the sketch name i.e. ((SketchName)applet).touchDown( ID, xPos, yPos, xWidth, yWidth );
		// Make sure applet is defined as PApplet and that 'applet = this;' is in setup().
		//((OmicronTouchExample)applet).touchDown( ID, xPos, yPos, xWidth, yWidth );
		//((MainPage)applet).touchDown( ID, xPos, yPos, xWidth, yWidth );
		
		switch(page)
		{
		case 0:		
			System.out.println("Here" + xPos+","+yPos);
			if(xPos>width/4 && yPos>height/4 && xPos<width/2 && yPos<height/4+height/2)
			{	page = 1;
				break;
			}
			else if(xPos>width/2 && yPos>height/4 && xPos<width-width/4 && yPos<height/4+height/2)
			{	page = 2;
				break;
			}
			else if((int)xPos>(int)(.8*width) && (int)yPos>(int)(.8*height));
			{
				System.out.println(xPos+","+yPos);
				System.out.println((int)(.8*width)+","+(int)(.8*height));
				page = 3; 
				break;
			}
				
		case 1:		
			if(xPos>width-width/4 && yPos>height/4 && xPos<width-width/4+width/5 && yPos<height/4+height/2)
				page = 0;
				break;
				
		case 2:		
			if(xPos>width-width/4 && yPos>height/4 && xPos<width-width/4+width/5 && yPos<height/4+height/2)
				page = 0;
				break;
				
		case 3:		
			if(xPos>width-width/4 && yPos>height/4 && xPos<width-width/4+width/5 && yPos<height/4+height/2)
				page = 0;
				break;
		}
		//if(initialMillis == 0) initialMillis = System.currentTimeMillis();
	}// touchDown
	
	// Called on a touch move event
	// mouseDragged events also call this with an ID of -1 and an xWidth and yWidth of 10.
		public void touchMove(int ID, float xPos, float yPos, float xWidth, float yWidth)
		{
			this.fill(0,255,0);
			this.noStroke();
			this.ellipse( xPos, yPos, xWidth, yWidth );
		
		
//		if(initialMillis == 0){
//			initialMillis = System.currentTimeMillis();
//			System.out.println("Initialisied in move event:" + initialMillis);
//		}
//		else{
//			count = System.currentTimeMillis() - initialMillis;
//			System.out.println(count);	
//		}
//		//System.out.println(initialMillis);
//		
//		if(count > 5000){
//			System.out.println("This is a long  touch event! handle it!");
//			initialMillis = 0;
//			count = 0;
//		}
//		
		
		//((MainPage)applet).touchMove( ID, xPos, yPos, xWidth, yWidth );
		}// touchMove

	// Called on a touch up event
	// mouseReleased events also call this with an ID of -1 and an xWidth and yWidth of 10.
		public void touchUp(int ID, float xPos, float yPos, float xWidth, float yWidth)
		{
//		initialMillis = 0;
			this.fill(0,0,255);
			this.noStroke();
			this.ellipse( xPos, yPos, xWidth, yWidth );

		//((MainPage)applet).touchUp( ID, xPos, yPos, xWidth, yWidth );
		}// touchUp

		void drawAxisLabels(String label, float plotX1, float plotY1, float plotX2, float plotY2, float labelX, float labelY) 
		  {
		    fill(0,255,0);
		    //textSize(.011*height);
		    textLeading(15);

		    textAlign(CENTER, CENTER);
		    // Use \n (enter/linefeed) to break the text into separate lines
		    text(label, labelX, (plotY1+plotY2)/2);
		    textAlign(CENTER);
		    text("Year(s)", (plotX1+plotX2)/2, labelY);
		  }

		  void drawYearLabels(int yearMin, int yearMax, float plotX1, float plotY1, float plotX2, float plotY2) 
		  {
		    fill(0,255,0);
		    //textSize(5);
		    textAlign(CENTER, BOTTOM);

		    // Use thin, gray lines to draw the grid
		    stroke(224);
		    strokeWeight(1);

		    for (int row = 0; row < rowCount; row++) 
		    {
		      if (years[row] % yearInterval == 0) 
		      {  
		        float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
		        if (years[row]%2==0)
		        {
		          textAlign(CENTER, BOTTOM);
		          text(years[row], x, (float)(plotY2));
		        }
		        else
		        {
		          textAlign(CENTER, TOP);
		          text(years[row], x, (float)(plotY2));
		        }
		        line(x, plotY1, x, plotY2);
		      }
		    }
		  }
		  
		  void drawVolumeLabels(int dataMin, int dataMax, int volumeInterval, int volumeIntervalMinor, float plotX1, float plotY1, float plotX2, float plotY2) 
		  {
		    fill(0,255,0);
		    //textSize(0.011*height);

		    stroke(128);
		    strokeWeight(1);

		    for (float v = dataMin; v <= dataMax; v += volumeIntervalMinor) 
		    {
		      if (v % volumeIntervalMinor == 0) {     // If a tick mark
		        float y = map(v, dataMin, dataMax, plotY2, plotY1);  
		        if (v % volumeInterval == 0) {        // If a major tick mark
		          if (v == dataMin) {
		            textAlign(RIGHT);                 // Align by the bottom
		          } 
		          else if (v == dataMax) {
		            textAlign(RIGHT, TOP);            // Align by the top
		          } 
		          else {
		            textAlign(RIGHT, CENTER);         // Center vertically
		          }
		          text(floor(v), plotX1 - 10, y);
		          line(plotX1 - 4, y, plotX1, y);     // Draw major tick
		        } 
		        else
		        {
		          // Commented out, too distracting visually
		          line(plotX1 - 2, y, plotX1, y);   // Draw minor tick
		        }
		      }
		    }
		  }
		  
		  void drawDataPointsTEP(int col, int dataMin, int dataMax, float plotX1, float plotY1, float plotX2, float plotY2, int r, int g, int b) 
		  {
			stroke(r, g, b);
		    strokeWeight(10);

		    for (int row = 0; row < rowCount; row++) 
		    {
		      if (dataTEP.isValid(row, col)) 
		      {
		        float value = dataTEP.getFloat(row, col);
		        float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
		        float y = map(value, dataMin, dataMax, plotY2, plotY1);
		        if (value >= dataMinTEP && value <= dataMaxTEP)
		        {
		          point(x, y);
		        }
		      }
		    }
		    
		    strokeWeight(10);
		  }
}// TouchListener
